package org.geemp.game.controller;

import org.geemp.game.common.CommonResult;
import org.geemp.game.gm.service.GameDBServiceImpl;
import org.geemp.game.service.BaseDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JackRen
 * @date 2021-10-26 10:15
 * @description:
 */

@Controller
public class LoginController {

    @Autowired
    private BaseDBService baseDBService;
    @Autowired
    private GameDBServiceImpl gameDBService;

    @RequestMapping("/")
    public String main(){
        return "main";
    }
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("/webup")
    public String webup(){
        return "webup";
    }

    @RequestMapping("/open/{slottype}")
    public ModelAndView open(@PathVariable String slottype){
        System.out.println("=----------"+slottype);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("slottype",slottype);
        modelAndView.setViewName("open");
        return modelAndView;
    }
    @RequestMapping("/optionvalue")
    public String optionvalue(){
        return "optionvalue";
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }

    /**
     * 登录界面
     * @return
     */



    /**
     * 注册界面
     * @return
     */
    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/success")
    public String success(){
        return "success";
    }

    /**
     * 登录接口
     * @param request
     * @return
     */
    @RequestMapping(value = "/login/doLogin",method = RequestMethod.POST)
    @ResponseBody
    public Object doLogin(HttpServletRequest request){
        String ut_userid = request.getParameter("ut_userid");
        String ut_password = request.getParameter("ut_password");
        String ut_role = request.getParameter("ut_role");
        if (StringUtils.isEmpty(ut_userid) || StringUtils.isEmpty(ut_password) || StringUtils.isEmpty(ut_role)) {
            return CommonResult.failed();
        }
        String sql = "select * from usertable where ut_userid='"+ut_userid+"' and ut_password='"+ut_password+"'";
        int ret = baseDBService.excuteQuerySqlCount(sql);
        if (ret > 0) {
            String roleSql = "select * from userchar where uc_charname='"+ut_role+"'";
            Map<String,Object> map = gameDBService.obtainChartIndexBy(ut_userid,ut_role);
            if (map !=null) {
                HttpSession session =request.getSession();
                session.setAttribute("ut_userid",ut_userid);
                session.setAttribute("ut_role",ut_role);
                session.setMaxInactiveInterval(0);
                return CommonResult.success(null,null);
            }else {
                return CommonResult.failed("角色不正确");
            }
        }else{
            return CommonResult.failed("用户名或密码不正确");
        }
    }
    @RequestMapping(value = "doUserStutas",method = RequestMethod.POST)
    @ResponseBody
    public Object doUserStutas(HttpServletRequest request) {
        HttpSession session =request.getSession();
        if (session.getAttribute("ut_userid")==null || session.getAttribute("ut_role")==null) {
            return CommonResult.failed("请先登录用户");
        }
        String CT_USERID = (String) session.getAttribute("ut_userid");
        String CT_NAME = (String) session.getAttribute("ut_role");
//        String CT_USERID = request.getParameter("CT_USERID");                 //关联登录账户属性参数userid
//        String sql = "select CT_INGOT,CT_GOLD,CT_POINT from chartable where  CT_USERID='"+CT_USERID+"' and CT_NAME='"+CT_NAME+"'"; //查询这个userid的属性表单
        List<Map<String, Object>> rs = gameDBService.obtainDataBy(CT_USERID,CT_NAME);       //查询方法
        if (rs.size() >0) {
            Map<String ,Object> map = rs.get(0);
            map.put("CT_USERID",CT_USERID);
            map.put("CT_NAME",CT_NAME);
            return CommonResult.success(map);//返回结果
        }
       return  CommonResult.success(new HashMap<>());
    }
}


